/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Serialization;

import Domain.Data;
import java.beans.XMLEncoder;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Memet
 */
public class Serializer {

    /**
     * Slaat het programma op
     * @param data de Database
     */
    public static void Save(Data data) {
        Data e = data;

        try {
            FileOutputStream fileOut =
                    new FileOutputStream("data.lcp");
            ObjectOutputStream out =
                    new ObjectOutputStream(fileOut);
            out.writeObject(e);
            out.close();
            fileOut.close();
        } catch (IOException i) {
            i.printStackTrace();
        }
    }

    /**
     * Parsed een XML file
     * @param data database
     * @param filename de naam van het bestand
     * @deprecated XML wordt nu handmatig geparsed vanwege de diversiteit in output
     */
    @Deprecated
    public static void XMLSave(Object data, String filename) {
        Object e = data;

        try {
            FileOutputStream fileOut =
                    new FileOutputStream(filename);
            XMLEncoder out =
                    new XMLEncoder(fileOut);
            out.writeObject(e);
            out.close();
            fileOut.close();
        } catch (IOException i) {
            i.printStackTrace();
        }
    }

    /**
     * Laad het programma in
     * @return de database
     */
    public static Data Laad() {
        Data e = null;
        try {
            FileInputStream fileIn =
                    new FileInputStream("data.lcp");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            e = (Data) in.readObject();
            in.close();
            fileIn.close();
        } catch (IOException i) {
            //i.printStackTrace();
        } catch (ClassNotFoundException c) {
            //c.printStackTrace();
        } finally {
            return e;
        }


    }

    /**
     * Laad het programma in
     * @return de database
     */
    public static Data Laad(String filename) {
        Data e = null;
        FileInputStream fileIn = null;
        ObjectInputStream in = null;
        try {

            String filenm = filename.substring(filename.lastIndexOf('\\') + 1, filename.length());
            filename = filename.substring(0, filename.lastIndexOf('\\') + 1);

            File f = new File(filename, filenm);

            fileIn = new FileInputStream(f);
            in = new ObjectInputStream(fileIn);
            e = (Data) in.readObject();
            in.close();
            fileIn.close();
        } catch (IOException i) {
            Logger.getLogger(Serializer.class.getName()).log(Level.SEVERE, null, i);
        } catch (ClassNotFoundException c) {
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ex) {
                    Logger.getLogger(Serializer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (fileIn != null) {
                try {
                    fileIn.close();
                } catch (IOException ex) {
                    Logger.getLogger(Serializer.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

            return e;
        }
    }
}
